home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / bicheck.rexx < prev    next >
OS/2 REXX Batch file  |  2000-11-28  |  5KB  |  141 lines

  1. /*fast broadcast icmp chech macro*/
  2.  
  3. call init
  4. call cycle
  5. exit
  6. /***************************************************************************/
  7. err: procedure expose global.
  8. parse arg msg,ntsockerr,ntprg
  9.     if ntsockerr~=1 then msg=msg "("errorstring(errno())")"
  10.     if ntprg~=1 then msg=global.prg":" msg
  11.     call writeln(global.stderr,msg)
  12.     exit
  13. /***************************************************************************/
  14. info: procedure expose global.
  15. parse arg msg
  16.     call writeln(global.stderr,msg)
  17.     return
  18. /***************************************************************************/
  19. getsuitable: procedure expose global.
  20.     res=QueryInterfaces("IN")
  21.     if res>0 then
  22.         do i=0 to res-1
  23.             if in.i.family==2 & in.i.UP==1 & in.i.LOOPBACK==0 then return in.i.IFADDRS
  24.         end
  25.     return -1
  26. /***************************************************************************/
  27. init: procedure expose global.
  28.     if ~open("GLOBAL.STDERR","CONSOLE:","W") then global.stderr="STDOUT"
  29.     l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then call err "can't find rmh.library any version",1,1
  30.     global.prg=programName("NOEXT")
  31.     if AddLibrary("rexxsupport.library","rxsocket.library","rxlibnet.library")~=0 then err "can't find" result
  32.     parm.1.value=3
  33.     if ~RMH_ReadArgs("FILE/A,TIMEOUT/N,VER=VERBOSE/S,MIN/K/N") then do
  34.         call printfault(ioerr(),global.prg)
  35.         exit
  36.     end
  37.     global.myAddr=getSuitable()
  38.     if global.myAddr=-1 then call err "no suitable device found",1
  39.     global.file=parm.0.value
  40.     global.timeout=parm.1.value
  41.     if global.timeout<0 then call err "bad timeout value '"global.timeout"'",1
  42.     global.ver=parm.2.flag
  43.     if parm.3.flag then
  44.         if parm.3.value<0 then err "bad min value '"parm.3.value"'",1
  45.         else global.minRet=parm.3.value
  46.     else global.minRet=0
  47.     if ~open("IN",global.file,"R") then call err "file '"global.file"' not found",1
  48.     global.sock=socket("INET","RAW","ICMP")
  49.     if global.sock<0 then call err "socket not created"
  50.     call setsockopt(global.sock,"SOCKET","BROADCAST",1)
  51.     call setsockopt(global.sock,"IP","HDRINCL",1)
  52.     call IOCtlSocket(global.sock,"FIONBIO",1)
  53.     global.tim=CreateTimer()
  54.     global.ts=TimerSignal(global.tim)
  55.     global.id=x2d(right(pragma("ID"),4))
  56.     global.ip.v   = 4
  57.     global.ip.hl  = 5
  58.     global.ip.tos = 0
  59.     global.ip.id  = global.id
  60.     global.ip.off = 0
  61.     global.ip.ttl = 65
  62.     global.ip.p   = 1
  63.     global.ip.src = global.myAddr
  64.     global.null=Null()
  65.     global.icmp.type=8
  66.     global.icmp.code=0
  67.     global.icmp.id=global.id
  68.     global.icmp.seq=0
  69.     global.sel.read.0=global.sock
  70.     global.sig=or(global.ts,2**12)
  71.     if global.ver then call info "File:"global.file "Timeout:"global.timeout "Addr:"global.myAddr
  72.     return
  73. /***************************************************************************/
  74. cycle: procedure expose global.
  75.     i=0
  76.     do while ~eof("IN")
  77.         w=-1
  78.         do k=0 to 19
  79.             drop global.ips
  80.             line=readln("IN")
  81.             if line~="" then do
  82.                 parse var line broad .
  83.                 parse var line broad "9"x
  84.                 broad=space(broad)
  85.                 i=i+1
  86.                 if isDotAddr(broad) then do
  87.                         w=w+1
  88.                         global.ips.w.name=broad
  89.                         global.ips.w.count=0
  90.                         call sendEchoReq(broad,w)
  91.                 end
  92.                 else say ";" left(broad,15) "*** bad dotted ***"
  93.             end
  94.         end
  95.         if w>=0 then do
  96.             call waitEcho(broad)
  97.             do k=0 to w
  98.                 if global.ips.k.count>=global.minRet then say "BROADCAST" left(global.ips.k.name,15) ";" global.ips.k.count
  99.             end
  100.         end
  101.     end
  102.     return
  103. /***************************************************************************/
  104. sendEchoReq: procedure expose global.
  105. parse arg broad,i
  106.     remote.addraddr=broad
  107.     global.ip.dst = broad
  108.     call GetSysTime("T")
  109.     global.icmp.icmpdata=right(global.null||d2c(t.secs),4) right(global.null||d2c(t.micro),4)
  110.     global.icmp.seq=i
  111.     icmpPkt=createicmp("GLOBAL.ICMP")
  112.     global.ip.len = 20+length(icmpPkt)
  113.     ipPkt=createip("GLOBAL.IP")
  114.     if global.ver then call info "sending eco request to <"broad"> ["i"]"
  115.     p=ipPkt||icmpPkt
  116.     global.plen=length(p)
  117.     if sendto(global.sock,p,0,"REMOTE")<0 then call err "error sending"
  118.     return
  119. /***************************************************************************/
  120. waitecho: procedure expose global.
  121.     if global.ver then call info " .waiting for eco reply"
  122.     do while 1
  123.         res=WaitSelect("GLOBAL.SEL",,,global.sig)
  124.         if and(global.sel.signals,2**12)~=0 then call err "user break",1
  125.         if res<0 then call err "error waitselect"
  126.         if res=0 then do
  127.             if global.timeout>0 then call StopTimer(global.tim)
  128.             return
  129.         end
  130.         if global.timeout>0 then call StartTimer(global.tim,global.timeout)
  131.         res=recvfrom(global.sock,"PKT",global.plen)
  132.         if res<0 then call "error recv"
  133.         parse var pkt ipPkt +20 t +1 c +1 ck +2 i +2 seq +2
  134.         if c2d(t)=0 & c2d(i)=global.id then do
  135.             s=c2d(seq)
  136.             global.ips.s.count=global.ips.s.count+1
  137.         end
  138.     end
  139.     /* not reached */
  140. /***************************************************************************/
  141.